// JavaScript Document
/**
* 用于显示缩略图列表和表格的复合型组件
* 同时创建两种列表展现组件，并且可以在两者之间进行切换
* 基于ligerImageGrid和ligerGrid实现
* 冀王历/2014-3-24
*/
function TableAndGrid(container, settings, layout){
	var defaultOptions = {
		width: 'auto', //宽度值
		height: 'auto', //宽度值
		idfield: null,
		parms: [],
		pageSizeOptions: [5, 10, 15, 20, 25],
		dataPageAction: 'server',
		pageSize: 15,
		checkbox: true,
		usePager: true,
		url: null,
		data: null,
		table: null,
		grid: null,
		isLayout: true,
		defaultView: 'grid',//grid/table
		dynamicToolbar: false,		//使用动态工具条，工具条显示属性：fix|single|multi
        dynamicToolbarRender: null	//使用自定义动态工具条处理程序
	};
	this.options = jQuery.extend({}, defaultOptions, settings);
	this.idfield = this.options.idfield;
	this.type = 'TableAndGrid';
	this._view = null;
	this.GRID = 'grid';
	this.TABLE = 'table';
	
	this._dataChanged = true; //数据是否改变
	
	this._curData = null;
	
	this.selected = [];
	
	//初始化
	this._init=function(){
		var g = this, p = this.options;
		
		if(p.table){//创建表格
			container.append('<div id="center_tablecon"></div>');
			g.tablecon = $("#center_tablecon", container);
			
			if($.ligerui.get(g.tablecon.attr("id")))
			{
				$.ligerui.remove(g.tablecon.attr("id"));//如果存在先移除
			}
			//列表参数
			p.table.dataPageAction = 'server';
			p.table.pageSize = p.pageSize;
			p.table.checkbox = p.checkbox;
			p.table.usePager = p.usePager;
			p.table.url = "#";
			
			//加载数据通知
			var tableOnSuccess = p.table.onSuccess;
			p.table.onSuccess = function(data, tg){
				g._reportSuccess(data, tg);
				
				if(typeof tableOnSuccess == 'function')
				{
					tableOnSuccess.call(this, data, tg);
				}
			};
			
			p.table.parms = p.parms;
			p.table.dynamicToolbar = p.dynamicToolbar;
			var tableDynamicToolbarRender = p.table.dynamicToolbarRender;
			p.table.dynamicToolbarRender = function(len, selecteds){
				g._dynamicToolbarRender(len, selecteds, this);
				
				if(typeof tableDynamicToolbarRender == 'function')
				{
					tableDynamicToolbarRender.call(this, len, selecteds);
				}
			};
			
			var tableOnAfterShowData = p.table.onAfterShowData;
			p.table.onAfterShowData = function(data){
				g._tableAfterShowData(data);
				
				if(typeof tableOnAfterShowData == 'function')
				{
					tableOnAfterShowData.call(this, data);
				}
			};
			
			g.table = g.tablecon.ligerGrid(p.table);
			g.useTable = true;
		}
		
		if(p.grid){//创建缩略图视图
			container.append('<div id="center_gridcon"></div>');
			g.gridcon = $("#center_gridcon", container);
			
			if($.ligerui.get(g.gridcon.attr("id")))
			{
				$.ligerui.remove(g.gridcon.attr("id"));//如果存在先移除
			}
			//列表参数
			p.grid.dataPageAction = 'server';
			p.grid.pageSize = p.pageSize;
			p.grid.checkbox = p.checkbox;
			p.grid.usePager = p.usePager;
			p.grid.url = "#";
			
			//加载数据通知
			var gridOnSuccess = p.grid.onSuccess;
			p.grid.onSuccess = function(data, tg){
				g._reportSuccess(data, tg);
				
				if(typeof gridOnSuccess == 'function')
				{
					gridOnSuccess.call(this, data, tg);
				}
			};
			
			p.grid.parms = p.parms;
			p.grid.dynamicToolbar = p.dynamicToolbar;
			var gridDynamicToolbarRender = p.grid.dynamicToolbarRender;
			p.grid.dynamicToolbarRender = function(len, selecteds){
				g._dynamicToolbarRender(len, selecteds, this);
				
				if(typeof gridDynamicToolbarRender == 'function')
				{
					gridDynamicToolbarRender.call(this, len, selecteds);
				}
			};
			
			var gridOnAfterShowData = p.grid.onAfterShowData;
			p.grid.onAfterShowData = function(data){
				g._gridAfterShowData(data);
				
				if(typeof gridOnAfterShowData == 'function')
				{
					gridOnAfterShowData.call(this, data);
				}
			};
			
			g.grid = g.gridcon.ligerImageGrid(p.grid);
			g.useGrid = true;
		}
		g.useAll = g.useTable && g.useGrid;
		g.setView(p.defaultView, true);
	};
	//切换视图
	this.setView = function(view, init)
	{
		var g = this, p = this.options;
		if(g._view == view)return;
		if(g.useAll)
		{
			//切换到缩略图
			if(view == g.GRID)
			{
				g.tablecon.hide();
				g.gridcon.show();
				g._view = g.GRID;
				g.toolbarManager = g.grid.toolbarManager;
			}
			//切换到表格
			else
			{
				g.gridcon.hide();
				g.tablecon.show();
				g._view = g.TABLE;
				g.toolbarManager = g.table.toolbarManager;
			}
		}
		//切换到表格
		else if(g.useTable)
		{
			g.tablecon.show();
			g._view = g.TABLE;
			g.toolbarManager = g.table.toolbarManager;
		}
		//切换到缩略图
		else if(g.useGrid)
		{
			g.gridcon.show();
			g._view = g.GRID;
			g.toolbarManager = g.grid.toolbarManager;
		}
		
		if(layout){
			layout._gvDefaultView = g._view;
			$.cookie('_gvdv', g._view);
		}
		//加载数据
		if(init){//初始化
			if(g._view == g.GRID)
			{
				g.grid.set('url', p.url);
			}
			else if(g._view == g.TABLE)
			{
				g.table.set('url', p.url);
			}
		}else{
			//手动切换处理
			if(!g._dataChanged){//如果数据没有变化时不重新加载
				if(g._view == g.GRID)
				{
					g._gridAfterShowData();
				}
				else if(g._view == g.TABLE)
				{
					g._tableAfterShowData();
				}
				
				return;
			}
			
			//从表格切换到缩略图
			if(g._view == g.GRID)
			{
				if(!g.grid.options.url || g.grid.options.url=='#'){//第一次加载数据时设置url
					g.grid.options.url = p.url;
				}
				
				if(!g.useTable)return;
				//列表参数同步
				var parms = g.table.get('parms');
				var newPage = g.table.get('newPage');
				var pageSize = g.table.get('pageSize');
				var sortName = g.table.get('sortName');
				var sortOrder = g.table.get('sortOrder');
				
				var opts = {parms: parms, newPage: newPage, pageSize:pageSize, sortName:sortName, sortOrder:sortOrder};
				
				g.grid.set(opts);
				
				var _data = jQuery.extend(true, {}, g._curData);
				
				g.grid.set('data', _data);
				
				g._dataChanged = false;
			}
			//从缩略图切换到表格
			else if(g._view == g.TABLE)
			{
				if(!g.table.options.url || g.table.options.url=='#'){//第一次加载数据时设置url
					g.table.options.url = p.url;
				}
				if(!g.useGrid)return;
				//列表参数同步
				var parms = g.grid.get('parms');
				var newPage = g.grid.get('newPage');
				var pageSize = g.grid.get('pageSize');
				var sortName = g.grid.get('sortName');
				var sortOrder = g.grid.get('sortOrder');
				
				var opts = {parms: parms, newPage: newPage, pageSize:pageSize, sortName:sortName, sortOrder:sortOrder};
				
				g.table.set(opts);
				
				var _data = jQuery.extend(true, {}, g._curData);
				
				g.table.set('data', _data);
				
				g._dataChanged = false;
			}
		}
	};
	//加载数据
	this.loadData = function(loadDataParm)
	{
		var g = this, p = this.options;
		if(g._view == g.GRID)
		{
			if(!g.grid.options.url || g.grid.options.url=='#'){//第一次加载数据时设置url
				g.grid.options.url = p.url;
			}
			g.grid.options.dataAction = "server";
			g.grid.options.dataType = "server";
			
			g.grid.loadData(loadDataParm);
		}
		else if(g._view == g.TABLE)
		{
			if(!g.table.options.url || g.table.options.url=='#'){//第一次加载数据时设置url
				g.table.options.url = p.url;
			}
			g.table.options.dataAction = "server";
			g.table.options.dataType = "server";
			
			g.table.loadData(loadDataParm);
		}
	};
	//获取选中的数据
	this.getSelecteds = function()
	{
		var g = this;
		if(g._view == g.GRID)
		{
			return g.grid.getSelecteds();
		}
		else if(g._view == g.TABLE)
		{
			return g.table.getSelecteds();
		}
		return [];
	};
	//设置列表参数
	this.setOptions = function(arg, value)
	{
		this.set(arg, value);
	};
	//设置列表参数
	this.set = function(arg, value)
	{
		var g = this, p = this.options;
		if(g._view == g.GRID)
		{
			g.grid.set(arg, value);
		}
		else if(g._view == g.TABLE)
		{
			g.table.set(arg, value);
		}
	};
	//页面大小发生变化时重新设置表格大小
	this._onResize = function()
	{
		var g = this, p = this.options;
		if(g.useTable)g.table._onResize();
		if(g.useGrid)g.grid._onResize();
	};
	//报告加载数据状态
	this._reportSuccess = function(data, tg)
	{
		var g = this, p = this.options;
		if (!data){
			delete g._curData;
			g._curData = {};
		}else{
			delete g._curData;
			g._curData = jQuery.extend(true, {}, data);
		}
		g._dataChanged = true;
		g.selected = [];
	};
	
	//表格显示后处理
	this._tableAfterShowData = function(data)
	{
		var g = this, p = this.options;
		
		var selectedIds = [];
		if(g.selected){
			for(var i in g.selected){
				selectedIds.push(g.selected[i][p.idfield]);
			}
		}
		if(g.table){
			g.table.selectByDataIds(selectedIds, p.idfield);
		}
	};
	//缩略图显示后处理
	this._gridAfterShowData = function(data)
	{
		var g = this, p = this.options;
		
		var selectedIds = [];
		if(g.selected){
			for(var i in g.selected){
				selectedIds.push(g.selected[i][p.idfield]);
			}
		}
		if(g.grid){
			g.grid.selectByDataIds(selectedIds, p.idfield);
		}
	};
	//选中回调函数，当列表选中状态发生变化时调用
	this._dynamicToolbarRender = function(len, selecteds, tg)
	{
		var g = this, p = this.options;
		g.selected = selecteds;
		if(p.dynamicToolbarRender){
			p.dynamicToolbarRender.call(g, len, selecteds);
		}
	};
	
	this.getDataList = function(){
		var g = this;
		if(g._view == g.GRID)
		{
			return g.grid.getDataList();
		}
		else if(g._view == g.TABLE)
		{
			return g.table.getDataList();
		}
		return [];
	};
	
	this.destroy = function(){
		if(this.table){
			this.table.destroy();
		}
		if(this.grid){
			this.grid.destroy();
		}
		this._destroyed = true;
		container.empty();
		delete this.options;
	};
	
	this._init();
}
